//postcss 的插件呢vite内置了postCss 无需安装
import { Plugin } from 'postcss'

const Options = {
    viewportWidth: 375,
}
interface Options {
    viewportWidth: number
}
export const PostCsspxToViewport = (options: Options = Options): Plugin => {
    const opt = Object.assign({}, Options, options)
    return {
        //钩子函数
        postcssPlugin: 'postcss-px-to-viewport',
        Declaration(node) {
            // console.log(node.prop, node.value)
            if (node.value.includes('px')) {
                //考虑到有小数
                const num = parseFloat(node.value)
                node.value = `${((num / opt.viewportWidth) * 100).toFixed(2)}vw`
            }
        }
    }
}
